<?php
class ZeusLib_Helper_File
{

	/**
	 * Scans a directory without dots and returns it as array
	 *
	 * @param string $path
	 * @return array
	 */
	function scanDir($path)
	{
		// Create a result cache
		$result = array();

		// Open the path
		$scan = scandir($path);

		// Iterate all scanned dirs and files
		foreach($scan as $line)
		{
			// Skip the dots
			if($line == '.' || $line == '..')
			{
				continue;
			}

			// Add to result
			$result[] = $line;
		}

		// Return results
		return $result;
	}

	/**
	 * Checks whether the given path exists and is a php file.
	 *
	 * @param string $path
	 * @return boolean
	 */
	function isPhpFile($path)
	{
		// Return whether the file is a php file
		return self::isFile($path, 'php');
	}

	/**
	 * Checks whether the given path exists and is a xml file.
	 *
	 * @param string $path
	 * @return boolean
	 */
	function isXmlFile($path)
	{
		// Return whether the file is an xml file
		return self::isFile($path, 'xml');
	}

	/**
	 * Checks whether the given path is a file.
	 * 
	 * Can also check whether the given file also has a specific mimetype.
	 * 
	 * @param string $path
	 * @param string $mimeType
	 */
	function isFile($path, $mimeType = null)
	{
		// Check whether the path is a file
		if(is_file($path))
		{
			// Check whether no mimetype is given
			if($mimeType === null)
			{
				return true;
			}

			// Check whether the ending is of the given mimetype
			if(strrpos($path, '.'.$mimeType) == strlen($path) - 4)
			{
				// Return succes
				return true;
			}
		}

		// Return failure
		return false;
	}

	/**
	 * Write content into a file by given path.
	 *
	 * @param string $path
	 * @param string $content
	 */
	function writeFile($path, $content)
	{
		// Check whether the dir exists
		if(!file_exists(dirname($path)))
		{
			// Create the dir
			mkdir(dirname($path));
		}

		// Open the filestream
		$fp = fopen($path, 'w');

		// Write the text
		fwrite($fp, $content);

		// Close the stream
		fclose($fp);
	}

	/**
	 * Reads content of a file and returns it.
	 *
	 * @param string $path
	 * @return string
	 */
	function readFile($path)
	{
		// Return file content
		return file_get_contents($path);
	}

}
?>